package cn.script.game.opt;

import cn.script.call_api.OptApi;
import cn.script.domain.RangePoint;
import cn.script.domain.Skill;
import cn.script.game.GameInfo;
import cn.script.game.GameOpt;
import cn.script.util.Delay;
import cn.script.util.LogOut;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 周永波
 */
public class Attack implements OptApi {

    private GameInfo gi;

    private GameOpt go;

    /**
     * 攻击的怪物名称
     */
    private String attName;

    /**
     * 当前锁定怪物
     */
    private String nowAttName;
    /**
     * 0:无怪物  1:锁定到目标怪物 2:锁定的怪物不是目标怪物
     */
    private int lockType = -1;
    /**
     * 1:攻击开始 2:攻击结束
     */
    private int attStatus = 1;

    /**
     * 怪物的范围
     */
    private RangePoint range;

    private List<Skill> skills = new ArrayList<>();

    private int endNum = 0;

    private int timeOut = -1;

    public Attack(GameOpt go, String attName) {
        this.go = go;
        this.gi = go.gi();
        this.attName = attName;
    }

    public String getAttName() {
        return this.attName;
    }

    public Attack range(int x, int y, int x1, int y1) {
        this.range = new RangePoint(x, y, x1, y1);
        return this;
    }

    public Attack endNum(int endNum) {
        this.endNum = endNum;
        return this;
    }

    public Attack skills(Skill... skillArr) {
        if (skillArr.length == 0) {
            return this;
        }
        for (Skill skill : skillArr) {
            this.skills.add(skill);
        }
        return this;
    }

    public Attack timeOut(int timeOut) {
        this.timeOut = timeOut;
        return this;
    }

    /**
     * 锁定怪物
     */
    private void lock() {
        this.gi.wio().keypress("f3", 300);
        this.nowAttName = this.gi.attackName();
        if (this.nowAttName == null) {
            this.lockType = 0;
            return;
        }
        // 当前锁定的怪物是目标怪物
        if (this.nowAttName != null && this.nowAttName.contains(this.attName)) {
            this.lockType = 1;
            return;
        }
        this.lockType = 2;
    }

    public void init() {
        this.lockType = -1;
        this.attStatus = 1;
        this.live = -1;
        if (this.skills != null) {
            for (Skill skill : this.skills) {
                skill.skillInfo(new Skill.SkillInfo());
            }
        }
    }

    private int live = -1;

    /**
     * 攻击启动
     *
     * @return
     */
    public int ack() {
        this.init();
        long startTime = System.currentTimeMillis();
        while (true) {
            Delay.delay(10);
            if (this.lockType != 1 && this.timeOut > 0 && (System.currentTimeMillis() - startTime) >= this.timeOut) {
                LogOut.out("当前怪物攻击--%s--超时", this.attName);
                return 3;
            }
            // 攻击结束后的操作
            if (this.attStatus == 2) {
                if (this.endNum > 0) {
                    LogOut.out("怪物击杀完成进入倒计时");
//                    for (int i = 0; i < this.endNum; i++) {
//                        this.lock();
//                        this.releaseSkill(startTime);
//                        Delay.delay(200);
//                    }
                    Delay.delay(endNum*1000);
                    this.go.reset();
                }
                return 1;
            }
            // 锁定怪物
            if (this.lockType == -1 || this.lockType == 2) {
                this.lock();
            }
            // 捕捉不到怪物
            if (this.lockType == 0) {
                LogOut.out("当前捕捉不到怪物");
                return 2;
            }
            if (this.lockType == 1) {
                if (this.live == 0) {
                    this.attStatus = 2;
                    LogOut.out("怪物--%s--已kill", this.nowAttName);
                    continue;
                }
                int live_ = this.gi.liveVal();
                // 防止 血条为0  捕捉不到
                if (live_ == -1 && this.live >= 0) {
                    this.live = 0;
                    continue;
                }
                // 捕捉不到怪物
                if (live_ == -1) {
                    this.live = live_;
                    continue;
                }
                // 血条发生变化
                if (live_ != -1 && this.live != live_) {
                    this.live = live_;
                    LogOut.out("当前怪物--%s--的血量:%d", this.nowAttName, this.live);
                }
            }
            this.releaseSkill(startTime);


        }
    }

    /**
     * 释放技能
     */
    public void releaseSkill(long startTime) {
        int time = (int) (System.currentTimeMillis() - startTime);
        RangePoint rg = this.range;
        for (Skill skill : this.skills) {
            if (!skill.isRelease(time)) {
                continue;
            }
            if (skill.type() == 0) {
                if (rg == null) {
                    rg = skill.range();
                }
                this.gi.wio().moveToEx(rg, 50);
            }
            this.gi.wio().keypress(skill.key());
        }
    }

    @Override
    public int opt() {
        return this.ack();
    }


    public Attack skills(List<Skill> skills) {
        this.skills = skills;
        return this;
    }

}
